package me.zhengjie.modules.info.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.info.domain.Worker;
import me.zhengjie.modules.info.service.WorkerService;
import me.zhengjie.modules.info.service.dto.WorkerDTO;
import me.zhengjie.modules.info.service.query.WorkerQueryService;
import me.zhengjie.modules.system.service.DeptService;
import me.zhengjie.modules.system.service.dto.DeptDTO;
import me.zhengjie.modules.system.service.dto.JobDTO;
import me.zhengjie.modules.system.service.query.DeptQueryService;
import me.zhengjie.modules.system.service.query.JobQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import me.zhengjie.modules.system.service.dto.DictDetailDTO;
import me.zhengjie.modules.system.service.query.DictDetailQueryService;
import me.zhengjie.utils.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.sql.Timestamp;
import java.util.Date;
/**
* @author 张鹏
* @date 2019-05-09
*/
@RestController
@RequestMapping("api")
public class WorkerController {

    @Autowired
    private WorkerService workerService;

    @Autowired
    private WorkerQueryService workerQueryService;

    @Autowired
    private DeptQueryService deptQueryService;

    @Autowired
    private DictDetailQueryService dictDetailQueryService;

    @Autowired
    private JobQueryService jobQueryService;

    @Autowired
    private DeptService deptService;

    private static final String ENTITY_NAME = "worker";

    @Log("查询Worker")
    @GetMapping(value = "/worker")
    public ResponseEntity getWorkers(WorkerDTO resources, Pageable pageable){
        return new ResponseEntity(workerQueryService.queryAll(resources,pageable),HttpStatus.OK);
    }

    @Log("新增Worker")
    @PostMapping(value = "/worker")
    public ResponseEntity create(@Validated @RequestBody Worker resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setCreator(userDetails.getUsername());
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setCreateTime(new Timestamp(now.getTime()));
        resources.setUpdateTime(new Timestamp(now.getTime()));
        return new ResponseEntity(workerService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改Worker")
    @PutMapping(value = "/worker")
    public ResponseEntity update(@Validated @RequestBody Worker resources){
        if (resources.getId() == null) {
            throw new BadRequestException(ENTITY_NAME +" ID Can not be empty");
        }
        UserDetails userDetails = SecurityContextHolder.getUserDetails();
        resources.setUpdator(userDetails.getUsername());
        Date now = new Date();
        resources.setUpdateTime(new Timestamp(now.getTime()));
        workerService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除Worker")
    @DeleteMapping(value = "/worker/{id}")
    public ResponseEntity delete(@PathVariable Long id){
        workerService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("获取AccessDevice所需字典")
    @GetMapping(value = "/worker/dict")
    public ResponseEntity getDict() {
        Map<String, Object> map = new HashMap<>();
        DictDetailDTO isNoDto = new DictDetailDTO();
        isNoDto.setDictName("is_no");
        map.put("is_no", dictDetailQueryService.queryAll(isNoDto));
        map.put("dept",deptQueryService.queryAll(new DeptDTO()));
        Map deptTreeMap = (Map)deptService.buildTree(deptQueryService.queryAll(new DeptDTO()));
        map.put("deptTree",deptTreeMap.get("content"));
        map.put("job",jobQueryService.queryAll(new JobDTO()));
        return new ResponseEntity(map, HttpStatus.OK);
    }

    @Log("获取身份证阅读器")
    @GetMapping(value = "/worker/idimg")
    public ResponseEntity getImg() {
        Map<String, Object> map = new HashMap<>();
        map.put("idPic1","https://gss1.bdstatic.com/9vo3dSag_xI4khGkpoWK1HF6hhy/baike/crop%3D12%2C0%2C574%2C379%3Bc0%3Dbaike80%2C5%2C5%2C80%2C26/sign=0a4802a5aa1ea8d39e6d2e44aa3b0262/f9dcd100baa1cd114b39c73eb512c8fcc3ce2d24.jpg");
        map.put("idPic2","https://gss0.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/zhidao/wh%3D600%2C800/sign=73faa8fbc61349547e4be062667ebe60/3bf33a87e950352aeb7935725e43fbf2b2118bab.jpg");
        return new ResponseEntity(map, HttpStatus.OK);
    }
}